package com.example.mobile2121606109ycg0331homework;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private Button btnMainReset;
    private RadioGroup radGupMainOption1;
    private RadioButton radBtnMainOpi1;
    private RadioButton radBtnMainOpi2;
    private RadioButton radBtnMainOpi3;
    private RadioButton radBtnMainOpi4;
    private RadioGroup radGupMainOption2;
    private RadioButton radBtnMainOpi5;
    private RadioButton radBtnMainOpi6;
    private RadioButton radBtnMainOpi7;
    private RadioButton radBtnMainOpi8;
    private RadioGroup radGupMainOption3;
    private RadioButton radBtnMainOpi9;
    private RadioButton radBtnMainOpi10;
    private RadioButton radBtnMainOpi11;
    private RadioButton radBtnMainOpi12;
    private RadioGroup radGupMainOption4;
    private RadioButton radBtnMainOpi13;
    private RadioButton radBtnMainOpi14;
    private RadioButton radBtnMainOpi15;
    private RadioButton radBtnMainOpi16;
    private Button btnMainSubmit;
    private TextView tvMainFraction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        //设置4个RadioGroup选项组的监听器
        //第一个
        radGupMainOption1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioButton radioButton = findViewById(i);
                radioButton.getText().toString();
                Log.i("RadioButton", radioButton.getText().toString());
            }
        });
        //第二个
        radGupMainOption2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioButton radioButton = findViewById(i);
                radioButton.getText().toString();
                Log.i("RadioButton", radioButton.getText().toString());
            }
        });
        //第三个
        radGupMainOption3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioButton radioButton = findViewById(i);
                radioButton.getText().toString();
                Log.i("RadioButton", radioButton.getText().toString());
            }
        });
        //第四个
        radGupMainOption4.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioButton radioButton = findViewById(i);
                radioButton.getText().toString();
                Log.i("RadioButton", radioButton.getText().toString());
            }
        });
        //提交按钮及分数判断
        btnMainSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int option1 = radGupMainOption1.getCheckedRadioButtonId();
                int option2 = radGupMainOption2.getCheckedRadioButtonId();
                int option3 = radGupMainOption3.getCheckedRadioButtonId();
                int option4 = radGupMainOption4.getCheckedRadioButtonId();
                int fraction = 0;
                //分数判断
                //第一题
                if (option1 == R.id.radBtnMainOpi1){
                    fraction += 25;
                }
                if (option2 == R.id.radBtnMainOpi7){
                    fraction += 25;
                }
                if (option3 == R.id.radBtnMainOpi10){
                    fraction += 25;
                }
                if (option4 == R.id.radBtnMainOpi13){
                    fraction += 25;
                }
                tvMainFraction.setText(String.valueOf(fraction));
            }
        });

        //重置按钮
        btnMainReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                radBtnMainOpi1.setChecked(false);
                radBtnMainOpi2.setChecked(false);
                radBtnMainOpi3.setChecked(false);
                radBtnMainOpi4.setChecked(false);
                radBtnMainOpi5.setChecked(false);
                radBtnMainOpi6.setChecked(false);
                radBtnMainOpi7.setChecked(false);
                radBtnMainOpi8.setChecked(false);
                radBtnMainOpi9.setChecked(false);
                radBtnMainOpi10.setChecked(false);
                radBtnMainOpi11.setChecked(false);
                radBtnMainOpi12.setChecked(false);
                radBtnMainOpi13.setChecked(false);
                radBtnMainOpi14.setChecked(false);
                radBtnMainOpi15.setChecked(false);
                radBtnMainOpi16.setChecked(false);
                tvMainFraction.setText(null);
            }
        });
    }

    private void initView() {
        btnMainReset = (Button) findViewById(R.id.btnMainReset);
        radGupMainOption1 = (RadioGroup) findViewById(R.id.radGupMainOption1);
        radBtnMainOpi1 = (RadioButton) findViewById(R.id.radBtnMainOpi1);
        radBtnMainOpi2 = (RadioButton) findViewById(R.id.radBtnMainOpi2);
        radBtnMainOpi3 = (RadioButton) findViewById(R.id.radBtnMainOpi3);
        radBtnMainOpi4 = (RadioButton) findViewById(R.id.radBtnMainOpi4);
        radGupMainOption2 = (RadioGroup) findViewById(R.id.radGupMainOption2);
        radBtnMainOpi5 = (RadioButton) findViewById(R.id.radBtnMainOpi5);
        radBtnMainOpi6 = (RadioButton) findViewById(R.id.radBtnMainOpi6);
        radBtnMainOpi7 = (RadioButton) findViewById(R.id.radBtnMainOpi7);
        radBtnMainOpi8 = (RadioButton) findViewById(R.id.radBtnMainOpi8);
        radGupMainOption3 = (RadioGroup) findViewById(R.id.radGupMainOption3);
        radBtnMainOpi9 = (RadioButton) findViewById(R.id.radBtnMainOpi9);
        radBtnMainOpi10 = (RadioButton) findViewById(R.id.radBtnMainOpi10);
        radBtnMainOpi11 = (RadioButton) findViewById(R.id.radBtnMainOpi11);
        radBtnMainOpi12 = (RadioButton) findViewById(R.id.radBtnMainOpi12);
        radGupMainOption4 = (RadioGroup) findViewById(R.id.radGupMainOption4);
        radBtnMainOpi13 = (RadioButton) findViewById(R.id.radBtnMainOpi13);
        radBtnMainOpi14 = (RadioButton) findViewById(R.id.radBtnMainOpi14);
        radBtnMainOpi15 = (RadioButton) findViewById(R.id.radBtnMainOpi15);
        radBtnMainOpi16 = (RadioButton) findViewById(R.id.radBtnMainOpi16);
        btnMainSubmit = (Button) findViewById(R.id.btnMainSubmit);
        tvMainFraction = (TextView) findViewById(R.id.tvMainFraction);
    }
}